#include "maintain_progressbardelegate.h"

#include <QPainter>
#include <QApplication>

MaintainProgressBarDelegate::MaintainProgressBarDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}


void MaintainProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(1 == index.column()) {
        QStyleOptionProgressBar progressBarOption;
        progressBarOption.rect = option.rect.adjusted(4, 10, -4, -10);
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.textAlignment = Qt::AlignRight;
        progressBarOption.progress = index.model()->data(index).toInt();
//        progressBarOption.textVisible = true;
//        progressBarOption.text = tr("%1%").arg(progressBarOption.progress);

        painter->save();
        if (option.state & QStyle::State_Selected) {
            painter->fillRect(option.rect, option.palette.highlight());
            painter->setBrush(option.palette.highlightedText());
        }
        QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
        painter->restore();
    }
    else {
        return QItemDelegate::paint(painter, option, index);
    }
}

bool MaintainProgressBarDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    return QItemDelegate::editorEvent(event, model, option, index);
}
